function drawTrapezoid(dim,all_flag)

if dim == 3
    n=6;
    title_d='3';
    fn='frustum';
else
    n=4;
    title_d='2';
    fn='trapezoid';
end
d=1/n;

draw1level=0;
draw2face=0;

if all_flag
    draw1level=1;
    fn=[fn '_full'];
else
    draw2face=1;
end

figure('name',fn);

hold on ;
axis off ;
axis equal ;

edgecolor='b';
linewidth=4;
markersize2d=200;
markeredgecolor2d='k';
markerfacecolor2d='r';

mec='none';
mfc='r';
markersize3d=1/(1.5*n);
edgesize3d=1/(8*n);
phigran=24;
markerthetagran=24;
edgethetagran=24;
efc='b';
eec='none';

eas=0.9;
eds=0.9;
mas=0.5;
mds=0.5;

I = imread(['latex' filesep 'img' filesep 'img1.jpg']);
J=zeros(size(I,1),size(I,2),size(I,3),n);
for i = 1:n
    I = imread(['latex' filesep 'img' filesep 'img' int2str(i) '.jpg']);
    J(:,:,:,i)=I;
end

r=edgesize3d;
theta=0:pi/edgethetagran:2*pi;
X=r*cos(theta);
Y=r*sin(theta);

set(gcf,'renderer','OpenGL');
for x=n*d:n*d:n
    for y = -1/(2*d):1/(2*d)
        z=x;
        surf(x*[-ones(size(X)) ; ones(size(X))],[Y+y*d*x*2;Y+y*d*x*2],[X+z;X+z],...
            'FaceLighting','phong','EdgeLighting','phong',...
            'AmbientStrength',eas,'DiffuseStrength',eds,...
            'FaceColor',efc,'EdgeColor',eec);
        surf([Y+y*d*x*2;Y+y*d*x*2],x*[-ones(size(X)) ; ones(size(X))],[X+z;X+z],...
            'FaceLighting','phong','EdgeLighting','phong',...
            'AmbientStrength',eas,'DiffuseStrength',eds,...
            'FaceColor',efc,'EdgeColor',eec);
    end
end
for xx = -n:2*n*d:n
    for yy = -n:2*n*d:n
        surf([X;X+xx],[Y;Y+yy],n*[zeros(size(X)) ; ones(size(X))],...
        'FaceLighting','phong','EdgeLighting','phong',...
        'AmbientStrength',eas,'DiffuseStrength',eds,...
        'FaceColor',efc,'EdgeColor',eec);
    end  
end
yx=n;
for x=n*d:n*d:n
    if (draw2face || draw1level) && x~=yx && ~all_flag
        continue
    end
    yxx=-x+(n-1)*2*x*d;
    yyy=-x+(n-1)*2*x*d;
    for xx = -x:2*x*d:x
        if draw2face && xx~=yxx
            continue
        end
        for yy = -x:2*x*d:x
            if draw2face && yy~=yyy
                continue
            end
            zz = x;
            s=2*x/n;
            t=2*d;
            rx=xx/(2*x*d);
            ry=yy/(2*x*d);
            
            if xx ~= x && yy ~= x
                surf([xx,xx+s;xx,xx+s],[yy,yy;yy+s,yy+s],zz*ones(2),J(:,:,:,x),...
                'FaceLighting','phong','EdgeLighting','phong',...
                'AmbientStrength',mas,'DiffuseStrength',mds,...
                'EdgeColor',mec,'FaceColor','texturemap');
                if draw2face
                    xx1=-(x-1)+((xx+x)/(2*x*d))*2*(x-1)*d;
                    yy1=-(x-1)+((yy+x)/(2*x*d))*2*(x-1)*d;
                    zz1=x-1;
                    s1=2*(x-1)/n;
                    surf([xx1,xx1+s1;xx1,xx1+s1],[yy1,yy1;yy1+s1,yy1+s1],zz1*ones(2),J(:,:,:,x),...
                    'FaceLighting','phong','EdgeLighting','phong',...
                    'AmbientStrength',mas,'DiffuseStrength',mds,...
                    'EdgeColor',mec,'FaceColor','texturemap');
                end
            end
            
            if xx ~= x
                surf([xx,xx+s;xx-rx*t,xx-(1+rx)*t+s],[yy,yy;yy-ry*t,yy-ry*t],[zz,zz;zz-1,zz-1],J(:,:,:,x),...
                'FaceLighting','phong','EdgeLighting','phong',...
                'AmbientStrength',mas,'DiffuseStrength',mds,...
                'EdgeColor',mec,'FaceColor','texturemap');
                if draw2face
                    s1=2*(x-1)/n;
                    surf([xx,xx+s;xx-rx*t,xx-(1+rx)*t+s],[yy+s,yy+s;yy-ry*t+s1,yy-ry*t+s1],[zz,zz;zz-1,zz-1],J(:,:,:,x),...
                    'FaceLighting','phong','EdgeLighting','phong',...
                    'AmbientStrength',mas,'DiffuseStrength',mds,...
                    'EdgeColor',mec,'FaceColor','texturemap');
                end
            end
        
            if yy ~= x
                surf([xx,xx;xx-rx*t,xx-rx*t],[yy,yy+s;yy-ry*t,yy-(1+ry)*t+s],[zz,zz;zz-1,zz-1],J(:,:,:,x),...
                'FaceLighting','phong','EdgeLighting','phong',...
                'AmbientStrength',mas,'DiffuseStrength',mds,...
                'EdgeColor',mec,'FaceColor','texturemap');
                if draw2face
                    s1=2*(x-1)/n;
                    surf([xx+s,xx+s;xx-rx*t+s1,xx-rx*t+s1],[yy,yy+s;yy-ry*t,yy-(1+ry)*t+s],[zz,zz;zz-1,zz-1],J(:,:,:,x),...
                    'FaceLighting','phong','EdgeLighting','phong',...
                    'AmbientStrength',mas,'DiffuseStrength',mds,...
                    'EdgeColor',mec,'FaceColor','texturemap');
                end
            end
        
        end
    end
end
if dim == 3
    view(60,15);
else
    view(0,0);
end
save_pic(fn);
end